//
//  NewsPresenter.m
//  News
//
//  Created by Han on 15-4-17.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "NewsPresenter.h"

@implementation NewsPresenter



- (void) getNewsList:(int)pager
{
    
    /*
    
    MKNetworkEngine *engine = [[MKNetworkEngine alloc] initWithHostName:@"192.168.2.176:3000" customHeaderFields:nil];
    MKNetworkOperation *op = [engine operationWithPath:@"/index" params:nil httpMethod:@"GET" ssl:NO];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"[operation responseData]-->>%@", [operation responseString]);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
    }];
    [engine enqueueOperation:op];
    [op cancel];
    */
    
    
    
    [self.delegate onLoadNewsListStart];
    
    
    
    

    [[HttpHelper sharedClient] GET:[NSString stringWithFormat:@"/v2/posts?type=news&page=%d", pager] parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {
        
        NSArray *responseData = [JSON valueForKeyPath:@"results"];
        
        NSMutableArray *newsList = [NSMutableArray arrayWithCapacity:[responseData count]];
        for (NSDictionary *attributes in responseData) {
            
            News *news = [[News alloc] init];
            news.title = [attributes valueForKeyPath:@"title"];
            news.news_id = [[attributes valueForKeyPath:@"id"] intValue];
            
            /*
            NSMutableString *img_url = [NSMutableString stringWithString: [attributes valueForKeyPath:@"imageUrl"]];
            
            NSString *search = @".jpg";
            NSString *replace = @",c_fill,h_150,w_200.jpg";
            NSRange substr = [img_url rangeOfString:search];
            if (substr.location != NSNotFound) {
                [img_url replaceCharactersInRange:substr withString:replace];      //把第1个遇到的substr替换为replace
            }
             
            
            news.img_url = img_url;
            */
            
            news.img_url = [attributes valueForKeyPath:@"imageUrl"];
             
            news.comment1 = [NSString stringWithFormat:@"%@%@", @"网易@擦肩而过：", news.title];
            news.comment2 = @"腾讯@神来7号：我发现西门子滚筒有件事情挺有意思。有次放了件掉色的红色衣服，洗出来结果把另一件纯白色的T染成了颜色均匀的粉红色。颜色均匀！也可以穿出街！为什么会这样？";
            news.comment3 = @"微信@唯美丶時尙沨〃：天啊！竟然发生在我家乡，希望有关单位能合法处理好，我愿意资助她，直到她立业成家！";
            
            [newsList addObject:news];
            
        }
        
        NewsListModel *newsListModel = NewsListModel.new;
        newsListModel.dataList = newsList;
        
        
        [self.delegate onLoadNewsListSuccess:newsListModel];
        
        [self.delegate onLoadNewsListFinish];
        
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        [self.delegate onLoadNewsListFailure];
        [self.delegate onLoadNewsListFinish];
    }];
    
    
}


@end
